<template>
  <addEditDepartment ref="addEditDepartment" />
  <el-container>
    <el-main>
      <div class="teacher">
        <div class="app-table">
          <div class="app-body">
            <el-tabs v-model="form.campus" type="card" class="demo-tabs" @tab-change="changeCampus">
              <el-tab-pane :label="campusName" :name="1">
                <div class="app-search">
                  <el-form :inline="true" class="demo-form-inline" v-model="form">
                    <el-form-item label="部门名称">
                      <el-input placeholder="请输入部门名称" v-model="form.name" @keyup.enter="searchEnter"></el-input>
                    </el-form-item>

                    <el-form-item>
                      <el-button class="serach_btn" type="primary" plain @click="onSubmit">搜索</el-button>
                      <el-button class="add_btn" type="success" plain @click="add">添加</el-button>
                    </el-form-item>
                  </el-form>
                </div>
                <el-table :data="list" size="default" style="width: 100%;" :row-class-name="tableRowClassName">
                  <el-table-column prop="id" label="序号" width="60" />
                  <el-table-column prop="name" label="部门名称" min-width="150" />
                  <el-table-column prop="campus" label="校区" min-width="150">
                    <template #default="scope">
                      <p v-if="scope.row.campus == 1" style="margin: 0;">{{ campusName }}</p>
                      <p v-else style="margin: 0;">{{ campusName1 }}</p>
                    </template>
                  </el-table-column>
                  <el-table-column prop="leaderName2" label="二级资产会计" min-width="120" />
                  <el-table-column prop="leaderName3" label="总账会计" min-width="120" />
                  <el-table-column prop="leaderName4" label="主管领导" min-width="150" />
                  <el-table-column label="操作" width="220px">
                    <template #default="scope">
                      <el-button class="edit_btn" type="success" @click="editApp(scope.row)">编辑</el-button>
                      <el-button class="del_btn" type="danger" @click="deleteApp(scope.row.id)">删除</el-button>
                    </template>
                  </el-table-column>

                  <template v-slot:empty>
                    <el-empty description="暂无数据"></el-empty>
                  </template>
                </el-table>
                <el-config-provider :locale="locale">
                  <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
                    layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
                    @current-change="changeCurrentPage">
                  </el-pagination>
                </el-config-provider>
              </el-tab-pane>
              <el-tab-pane :label="campusName1" :name="2">
                <div class="app-search">
                  <el-form :inline="true" class="demo-form-inline" v-model="form">
                    <el-form-item label="部门名称">
                      <el-input placeholder="请输入部门名称" v-model="form.name" @keyup.enter="searchEnter"></el-input>
                    </el-form-item>

                    <el-form-item>
                      <el-button class="serach_btn" type="primary" plain @click="onSubmit">搜索</el-button>
                      <el-button class="add_btn" type="success" plain @click="add">添加</el-button>
                    </el-form-item>
                  </el-form>
                </div>
                <el-table :data="list" size="default" style="width: 100%;" :row-class-name="tableRowClassName">
                  <el-table-column prop="id" label="序号" width="60" />
                  <el-table-column prop="name" label="部门名称" min-width="150" />
                  <el-table-column prop="campus" label="校区" min-width="150">
                    <template #default="scope">
                      <p v-if="scope.row.campus == 1" style="margin: 0;">{{ campusName }}</p>
                      <p v-else style="margin: 0;">{{ campusName1 }}</p>
                    </template>
                  </el-table-column>
                  <el-table-column prop="leaderName2" label="二级资产会计" min-width="120" />
                  <el-table-column prop="leaderName3" label="总账会计" min-width="120" />
                  <el-table-column prop="leaderName4" label="主管领导" min-width="150" />
                  <el-table-column label="操作" width="220px">
                    <template #default="scope">
                      <el-button class="edit_btn" type="success" @click="editApp(scope.row)">编辑</el-button>
                      <el-button class="del_btn" type="danger" @click="deleteApp(scope.row.id)">删除</el-button>
                    </template>
                  </el-table-column>

                  <template v-slot:empty>
                    <el-empty description="暂无数据"></el-empty>
                  </template>
                </el-table>
                <el-config-provider :locale="locale">
                  <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
                    layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
                    @current-change="changeCurrentPage">
                  </el-pagination>
                </el-config-provider>
              </el-tab-pane>
            </el-tabs>
            <!-- <div class="app-search">
              <el-form :inline="true" class="demo-form-inline" v-model="form">
                <el-form-item label="部门名称">
                  <el-input placeholder="请输入部门名称" v-model="form.name" @keyup.enter="searchEnter"></el-input>
                </el-form-item>

                <el-form-item>
                  <el-button class="serach_btn" type="primary" plain @click="onSubmit">搜索</el-button>
                  <el-button class="add_btn" type="success" plain @click="add">添加</el-button>
                </el-form-item>
              </el-form>
            </div>
            <el-table :data="list" size="default" style="width: 100%;" :row-class-name="tableRowClassName">
              <el-table-column prop="id" label="序号" width="60" />
              <el-table-column prop="name" label="部门名称" min-width="150" />
              <el-table-column prop="campus" label="校区" min-width="150">
                <template #default="scope">
                  <p v-if="scope.row.campus == 1" style="margin: 0;">{{ campusName }}</p>
                  <p v-else style="margin: 0;">{{ campusName1 }}</p>
                </template>
              </el-table-column>
              <el-table-column prop="leaderName2" label="二级资产会计" min-width="120" />
              <el-table-column prop="leaderName3" label="总账会计" min-width="120" />
              <el-table-column prop="leaderName4" label="主管领导" min-width="150" />
              <el-table-column label="操作" width="220px">
                <template #default="scope">
                  <el-button class="edit_btn" type="success" @click="editApp(scope.row)">编辑</el-button>
                  <el-button class="del_btn" type="danger" @click="deleteApp(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>

              <template v-slot:empty>
                <el-empty description="暂无数据"></el-empty>
              </template>
            </el-table>
            <el-config-provider :locale="locale">
              <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
                layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
                @current-change="changeCurrentPage">
              </el-pagination>
            </el-config-provider> -->
          </div>
          <bottomView />
        </div>
      </div>
    </el-main>
  </el-container>
</template>

<script>
import addEditDepartment from "@/components/assets/addEditDepartment.vue";
import bottomView from "@/components/bottom.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture } from "@element-plus/icons-vue";
import { getCurrentInstance } from "vue";
import { decrypt } from "@/utils/dateFormat";
import { API_BASE_NAME2, API_BASE_NAME3 } from '../../constants';

export default {
  name: "BaseTeacherList",

  data() {
    return {
      type: 0,
      title: "",
      page: 0,
      pageSize: 10,
      total: 0,
      limit: 10,
      list: [],
      flagId: '',
      schoolNames: [],
      user: {},
      form: {
        name: "",
        campus: "",
      },
      selectData: {},
      mobileNumber: '',
      campusName: API_BASE_NAME2,
      campusName1: API_BASE_NAME3,
    };
  },
  components: {
    IconPicture,
    ElConfigProvider,
    ElMessageBox,
    ElMessage,
    bottomView, addEditDepartment,
  },

  setup() {
    const instance = getCurrentInstance()
    let schoolNames = instance.appContext.config.globalProperties.$schoolNames
    // console.log("schoolNames", schoolNames)
    return {
      locale: zhCn,
      schoolNames: schoolNames
    }
  },
  mounted() {
    this.user = JSON.parse(sessionStorage.getItem("user"))
    this.form.campus = this.user.campus
    this.typeChange();
    this.getDepartmentList();
  },
  watch: {
    // $route() {
    //   this.typeChange();
    //   this.getDepartmentList();
    // },
  },
  methods: {
    searchEnter: function (e) {
      // 使用 which 和 keyCode 属性来解决兼容问题
      var keyCode = window.event ? e.keyCode : e.which;
      var val = e.target.value;
      if (keyCode == 13 && val) {
        this.getDepartmentList();
      }
    },
    add() {
      this.$refs.addEditDepartment.title = "添加" + this.title
      this.$refs.addEditDepartment.personnelType = this.type
      this.$refs.addEditDepartment.dialogFormVisible = true;
      this.$refs.addEditDepartment.getBaseTeacherList()
    },
    editApp(obj) {
      this.$refs.addEditDepartment.title = "修改信息"
      this.$refs.addEditDepartment.schoolNames = this.schoolNames
      this.$refs.addEditDepartment.getDepartmentById(obj.id);
      this.$refs.addEditDepartment.dialogFormVisible = true;
      this.$refs.addEditDepartment.id = obj.id;
      this.$refs.addEditDepartment.getBaseTeacherList()
    },


    deleteApp(id) {
      ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpService("/javaApi/faDepartment/deleteById", "id=" + id, "post").then(
            (data) => {
              if (data.code == 200) {
                ElMessage.success("删除成功");
                this.getDepartmentList();
              } else {
                ElMessage.error(data.message);
              }
            }
          );
        })
        .catch(() => { });
    },

    exportExcel() {
      httpService(
        "/javaApi/baseTeacher/exportBaseTeacher",
        {
          name: this.form.name,
          teacherNumber: this.form.teacherNumber,
          personnelType: this.type,
        },
        "get"
      ).then((data) => {
        if (data.code == 200) {
          window.open(data.object, '_blank')
        }
      });
    },
    onSubmit() {
      this.getDepartmentList();
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.pageSize = size;
      this.limit = size;
      this.getDepartmentList();
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.getDepartmentList();
    },
    tableRowClassName({ row, rowIndex }) {
      return "";
    },
    tableRowClassName({ row, rowIndex }) {
      return "";
    },
    changeCampus(value) {
      this.form.campus = value
      this.getDepartmentList()
    },
    getDepartmentList() {
      httpService(
        "/javaApi/faDepartment/list/" + this.page + "/" + this.pageSize,
        {
          name: this.form.name,
          campus: this.form.campus,
        },
        "get"
      ).then((data) => {
        this.list = data.list;
        this.total = data.total;
        this.pageCount = data.pages;
      });
    },
    typeChange() {
      this.type = this.$route.query.type;
    },
  },
};
</script>

<style scoped>
.el-main {
  padding: 0px;
  text-align: left
}

.teacher-content {
  width: 100%;
}

.app-table {
  background: #eff3f5;
  padding: 10px;
}

.app-separator {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 10px;
}

.app-body {
  background: white;
  /* padding: 20px 20px 10px 20px; */
  margin-bottom: 10px;
  min-height: calc(100vh - 214px);
}

.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
  text-align: center;
  margin-top: 10px;
}

.serach_btn {
  background-color: #007EFF;
  color: #fff;
  border-radius: 8px;
  border: none;
}

.add_btn {
  background-color: #00c78c;
  color: #fff;
  border-radius: 8px;
  border: none;
}

.edit_btn {
  background-color: #fff;
  color: #00c78c;
  border: none;
}

.del_btn {
  background-color: #fff;
  color: #FF0000;
  border: none;
}

.face_btn {
  background-color: #fff;
  color: #ff8400;
  border: none;
}

.retire_btn {
  background-color: #fff;
  color: #007EFF;
  border: none;
}

/* .app-search {
  padding-left: 20px;
} */
.el-tab-pane {
  padding: 0 10px;
}
</style>
